﻿Imports System.Windows.Forms
Imports System.IO
Imports modengine

Public Class mdiInterface
    ' Custom Font Loader
    Private FontRp As New modengine.CustomFont

    ' Private Variable to Store Working Directory, Init'd to Empty String
    Private WorkingDirectory As String = "Z:\Tool"

    Private Sub MDIParent1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Set Some Visual Properties
        Me.tvDirList.Font = FontRp.GetInstance(7.5, FontStyle.Regular)
        'If Mod Directory is Empty String
        If WorkingDirectory = "" Then
            'Clear the TreeView and Load from Current Launch Location
            Me.tvDirList.Nodes.Clear()
            lblWorkDir.Text = "Working Directory: " & My.Application.Info.DirectoryPath
            ModUtils.BuildTree(My.Application.Info.DirectoryPath, Me.tvDirList)
        Else
            'Otherwise, Use the Path of ModDirectory
            Me.tvDirList.Nodes.Clear()
            lblWorkDir.Text = "Working Directory: " & WorkingDirectory
            ModUtils.BuildTree(WorkingDirectory, Me.tvDirList)
        End If

    End Sub

    Private Sub ChangeDir(ByVal sender As Object, ByVal e As EventArgs) Handles OpenToolStripMenuItem.Click
        Dim OpenDir As New FolderBrowserDialog
        OpenDir.SelectedPath = WorkingDirectory
        If (OpenDir.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
            WorkingDirectory = OpenDir.SelectedPath
            Me.tvDirList.Nodes.Clear()
            lblWorkDir.Text = "Working Directory: " & WorkingDirectory
            ModUtils.BuildTree(WorkingDirectory, Me.tvDirList)
        End If
    End Sub

    Private Sub Export_Click(ByVal sender As Object, ByVal e As EventArgs) Handles SaveAsToolStripMenuItem.Click
        'Dim SaveFileDialog As New SaveFileDialog
        'SaveFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        'SaveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"

        'If (SaveFileDialog.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
        'Dim FileName As String = SaveFileDialog.FileName
        '' TODO: Add code here to save the current contents of the form to a file.
        'End If
    End Sub


    Private Sub ExitToolsStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub tvDirList_BeforeSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles tvDirList.BeforeSelect

    End Sub

    Private Sub tvDirList_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvDirList.AfterSelect

    End Sub

    Private Sub tvDirList_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tvDirList.DoubleClick
        ' When the TreeView is double clicked on, check for a selection
        If tvDirList.SelectedNode.IsSelected And tvDirList.SelectedNode.SelectedImageIndex >= 2 Then
            ' If we have a Node Selected
            ' Loop through the Child Controls, Check for a Tag (File Name)
            ' Which Matches another Child Control. If so, we Abort
            For Each ctlChild In Me.tdcEditPane.Items
                If ctlChild.Tag = tvDirList.SelectedNode.Name Then
                    MsgBox("File Already Open.", MsgBoxStyle.OkOnly)
                    Exit Sub
                End If
            Next
            ' If the File pointed to by the name of the Node exists
            If File.Exists(tvDirList.SelectedNode.Name) Then
                ' Convience Variables
                Dim FName As String = tvDirList.SelectedNode.Name
                Dim fInfo As New System.IO.FileInfo(FName)
                'Check for extensions
                If fInfo.Extension = ".rbf" Then
                    FName = FileOps.RBFtoText(FName)
                    If FName <> "" Then
                        ' If the Conversion worked, 
                        ' Create a new editor control and add it to the panel
                        'Dim ctlEdit As New ctlEditor(FName, tvDirList.SelectedNode.Text, tvDirList.SelectedNode.Name)
                        Dim wpfEdit As New modengine.wpfEditor(FName, tvDirList.SelectedNode.Text, tvDirList.SelectedNode.Name)
                        tdcEditPane.Items.Add(wpfEdit)
                        tdcEditPane.SelectedControl = wpfEdit
                    Else
                        MsgBox("Error in RBF conversion!", MsgBoxStyle.Critical)
                        Exit Sub
                    End If
                ElseIf Array.IndexOf(ModUtils.TextExts, fInfo.Extension) >= 0 Then
                    ' If we don't need any conversions then just go ahead and
                    ' Create a new editor control and add it to the panel
                    'Dim ctlEdit As New ctlEditor(FName, tvDirList.SelectedNode.Text)
                    Dim wpfEdit As New modengine.wpfEditor(FName, tvDirList.SelectedNode.Text, tvDirList.SelectedNode.Name)
                    tdcEditPane.Items.Add(wpfEdit)
                    tdcEditPane.SelectedControl = wpfEdit
                Else
                    MsgBox("This is a binary file and cannot be edited directly.")
                End If
            Else
                MsgBox("Unable to Open File", MsgBoxStyle.Critical, "Error Opening File")
            End If
        End If
    End Sub

    Private Sub RefreshDirectoryViewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshDirectoryViewToolStripMenuItem.Click
        Me.tvDirList.Nodes.Clear()
        lblWorkDir.Text = "Working Directory: " & WorkingDirectory
        ModUtils.BuildTree(WorkingDirectory, Me.tvDirList)
    End Sub

    Private Sub tdcEditPane_DocumentAdded(ByVal documentControl As Darwen.Windows.Forms.Controls.TabbedDocuments.TabbedDocumentControl, ByVal control As System.Windows.Forms.Control) Handles tdcEditPane.DocumentAdded
        Me.SaveToolStripButton.Enabled = True
    End Sub

    Private Sub tdcEditPane_DocumentRemoved(ByVal documentControl As Darwen.Windows.Forms.Controls.TabbedDocuments.TabbedDocumentControl, ByVal control As System.Windows.Forms.Control) Handles tdcEditPane.DocumentRemoved
        Dim ctlEdit As wpfEditor = control
        If ctlEdit.WorkUnsaved Then
            Dim result As MsgBoxResult = MsgBox("Do you wish to save your work?", MsgBoxStyle.YesNo, "Unsaved Work")
            If result = MsgBoxResult.Yes Then
                'ctlEdit.Tag should be the orginal file, ctlEdit.FileName is the actual file loaded
                ctlEdit.SaveFile()
            End If
        End If
        If documentControl.Items.Count < 1 Then
            Me.SaveAsToolStripMenuItem.Enabled = False
        End If
    End Sub

    Private Sub tdcEditPane_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles tdcEditPane.Paint
        For Each ctlEdit As wpfEditor In Me.tdcEditPane.Items
            If ctlEdit.WorkUnsaved Then
                tdcEditPane.ItemTitles.Item(ctlEdit) = "*" & ctlEdit.Name
            Else
                tdcEditPane.ItemTitles.Item(ctlEdit) = ctlEdit.Name
            End If
        Next
    End Sub

    Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click
        Dim ctlEdit As wpfEditor = Me.tdcEditPane.SelectedControl
        ' Call the Save File on the Control we define above from the active pane of the tabber
        ctlEdit.SaveFile()
    End Sub

    Private Sub SpawnTEsterToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim iForm As New Form1
        iForm.Show()
    End Sub
End Class
